package com.example.deertrip;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import com.example.deertrip.bean.Tb_HotelInfo;
import com.example.deertrip.database.DBOperate;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Manage_Hotel_Model extends AppCompatActivity {
    private List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
    private ListView listView;
    private SimpleAdapter adapter = null;
    private DBOperate dbOperate;
    List<HashMap<String, Object>> data= new ArrayList<HashMap<String, Object>>();//存放全部数据的列表
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_manage_hotel_model);

        listView=findViewById(R.id.manage_hotel_lv);
        dbOperate=new DBOperate(this);
        sql();
    }

    private void sql() {
        List<Tb_HotelInfo> HotelInfos = dbOperate.get_All_HotelInfo();

        Drawable dw;

        for (int i = 0; i < HotelInfos.size(); i++) {
            HashMap<String, Object> items = new HashMap<String, Object>(); //存放一条数据的哈希表

            Tb_HotelInfo HotelInfo = HotelInfos.get(i);
            BitmapDrawable bd = new BitmapDrawable(this.getResources(), HotelInfo.getHotelImage());//将Test对象中读取的点阵图片转换为bitmapdrawable
            dw = bd;    //再转换为Drawable
            items.put("HotelImage", dw);   //属性分别存入哈希表
            items.put("HotelAddress", HotelInfo.getHotelAddress());
            items.put("HotelScore", HotelInfo.getHotelScore());
            items.put("HotelName", HotelInfo.getHotelName());
            items.put("HotelTel", HotelInfo.getHotelTel());
            data.add(items);   //总数据哈希表中加入该条数据项
        }


        adapter = new SimpleAdapter(this, data, R.layout.hotel_info_bar_delete,
                new String[]{"HotelImage", "HotelAddress", "HotelScore", "HotelName", "HotelTel"}, new int[]{R.id.htimage, R.id.htaddress, R.id.htscore, R.id.htname, R.id.httel}) {
            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                final int p = position;
                Button deletebtn;// id为你自定义布局中按钮的id
                convertView = super.getView(position, convertView, parent);
                deletebtn = convertView.findViewById(R.id.hotel_delete);
                deletebtn.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        HashMap<String, Object> items = new HashMap<String, Object>();
                        items = (HashMap<String, Object>) getItem(p);
                        String id = (String) items.get("HotelName");
                        System.out.println(id);
                        System.out.println("点击了第" + p + "项");
                        dbOperate.delete_hotel(id);
                        data.remove(p);
                        adapter.notifyDataSetChanged();
                        Log.i("delete", "删除成功");
                        Toast.makeText(Manage_Hotel_Model.this, "删除成功！", Toast.LENGTH_LONG).show();
                    }
                });

                return convertView;
            }
        };

        adapter.setViewBinder(new SimpleAdapter.ViewBinder() {
            public boolean setViewValue(View view, Object data, String textRepresentation) {
                if (view instanceof ImageView && data instanceof Drawable) {
                    ImageView iv = (ImageView) view;
                    iv.setImageDrawable((Drawable) data);
                    return true;
                } else return false;
            }
        });

        listView.setAdapter(adapter);
    }

}